package wang.wangby.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CollectionUtil{
	public static List toList(Object[] objs){
		List list=new ArrayList();
		for(int i=0;i<objs.length;i++){
			list.add(objs[i]);
		}
		return list;
	}

	public static List sort(List list,final String field){
		Collections.sort(list,new MyComparator(field));
		return list;
	}

	//提取list里的某个字段用connector连接
	public static String pickUpField(List list,String field,String connector){
		StringBuilder sb=new StringBuilder();
		if(list==null||list.size()==0){
			return "";
		}
		for(Object o:list){
			Object v=BeanUtil.get(o,field);
			sb.append(v+connector);
		}
		return sb.substring(0,sb.length()-connector.length());
	}

	//提取list对象里的某个字段值
	public static List pickUpField(List list,String field){
		if(list==null){
			return new ArrayList();
		}
		List result=new ArrayList();
		for(Object o:list){
			Object v=BeanUtil.get(o,field);
			result.add(v);
		}
		return result;
	}

	//提取list里面么某个field对象的字段(value)值
	public static List pickUp(List list,String field,Object value){
		if(list==null){
			return new ArrayList();
		}
		List result=new ArrayList();
		for(Object o:list){
			Object v=BeanUtil.get(o,field);
			if(value.equals(v)){
				result.add(o);
			}
		}
		return result;
	}

	public static class MyComparator implements Comparator,Serializable{
		public String compareField;

		public MyComparator(String compareField){
			this.compareField=compareField;
		}

		public int compare(Object o1,Object o2){
			int result=0;
			Object obj1=BeanUtil.get(o1,compareField);
			Object obj2=BeanUtil.get(o2,compareField);
			if(obj1==null){
				return -1;
			}else if(obj2==null){
				return 1;
			}else if(obj1 instanceof Date){
				Date d1=(Date)obj1;
				Date d2=(Date)obj2;
				return d1.compareTo(d2);
			}else if(obj1 instanceof Integer){
				Integer d1=(Integer)obj1;
				Integer d2=(Integer)obj2;
				result=(d1-d2);
			}else{
				result=obj1.toString().compareTo(obj2.toString());
			}
			return result;
		}

	}

	/** 将list转为map */
	public static Map toMap(Collection list,String idName){
		if(list==null){
			return new HashMap();
		}
		Map map=new HashMap();
		for(Object o:list){
			try{
				Object id=BeanUtil.get(o,idName);
				map.put(id,o);
			}catch(Exception e){
				throw new RuntimeException(e.getMessage(),e);
			}
		}
		return map;
	}

	//将第二个list里的对象放到第一个里
	/**
	 * @param field 第一个对象里的字段名称
	 * @param key 两个对象关联的字段名
	 */
	public static void fill(List parent,List child,String field,String key){
		for(int i=0;i<parent.size();i++){
			Object par=parent.get(i);
			for(Object ch:child){
				Object p=BeanUtil.get(par,key);
				Object c=BeanUtil.get(ch,key);
				if(p==null||c==null){
					throw new RuntimeException("关联字段值不能为null:"+field+"."+key);
				}
				if(p.equals(c)){
					BeanUtil.set(par,field,ch);
					break;
				}

			}
		}
	}

}
